using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UPBLib { public static class Csv { public static string DECIMAL_SEPARATOR = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator; public static string CSV_DECIMAL_SEPARATOR = "."; public static string CSV_VALUES_SEPARATOR = ","; public static string CSV_NEWLINE = "\r\n"; public static string CSV_EXTENSION = "csv"; public static String FormatNumber(T value) { var sValue = value.ToString(); return sValue.Replace(DECIMAL_SEPARATOR, CSV_DECIMAL_SEPARATOR); } public static int ParseInt(string value) { return int.Parse(value); } public static double ParseDouble(string value) { value = value.Replace(CSV_DECIMAL_SEPARATOR, DECIMAL_SEPARATOR); return double.Parse(value); } public static decimal ParseDecimal(string value) { value = value.Replace(CSV_DECIMAL_SEPARATOR, DECIMAL_SEPARATOR); return decimal.Parse(value); } public static string CreateLine(params string[] values) { return string.Join(Csv.CSV_VALUES_SEPARATOR, values); } public static string[] SplitLine(string line) { return line.Split(CSV_VALUES_SEPARATOR[0]); } public static void Write(string file, IEnumerable values, string[] header) { using (var writer = new StreamWriter(file)) { if (header != null) { var headerLine = Csv.CreateLine(header); writer.WriteLine(headerLine); } foreach (var value in values) { var dataLine = Csv.CreateLine(value); writer.WriteLine(dataLine); } } } } }